package com.sfh.gd.service.impl;

import cn.dev33.satoken.util.SaResult;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sfh.gd.converter.GameConverter;
import com.sfh.gd.domain.dto.GameEditDTO;
import com.sfh.gd.domain.dto.GameStateDO;
import com.sfh.gd.domain.entity.*;
import com.sfh.gd.domain.dto.GameNewDTO;
import com.sfh.gd.domain.query.GamePageQuery;
import com.sfh.gd.domain.vo.GameVO;
import com.sfh.gd.mapper.*;
import com.sfh.gd.service.GameService;
import com.sfh.gd.util.StpKit;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;


import java.util.concurrent.*;

import java.time.LocalDateTime;

/**
 * 赛事
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GameServiceImpl extends ServiceImpl<GameMapper, GameDO> implements GameService {

    /**
     * 报名
     */
    private final GameRegistrationMapper gameRegistrationMapper;
    /**
     * 团队
     */
    private final GameTeamMapper gameTeamMapper;

    /**
     * PK
     */
    private final GamePKMapper gamePKMapper;


    @Override
    public SaResult getGamePage(GamePageQuery query) {
        //赛事列表
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtil.isNotEmpty(query.getName())) {
            queryWrapper.eq(GameDO::getName, query.getName());
        }
        if (StringUtil.isNotEmpty(query.getStatus())) {
            queryWrapper.eq(GameDO::getStatus, query.getStatus());
        }
        queryWrapper.orderBy(GameDO::getCreateTime);
        Page<GameDO> page = this.getMapper().paginate(query.getCurrent(), query.getPageSize(), queryWrapper);
        return SaResult.data(page);
    }

    @Override
    public SaResult addGame(GameNewDTO vo) {
        // 新建赛事实体
        GameDO gameDO = GameConverter.INSTANCE.doToDO(vo);
        gameDO.setStatus(1);
        gameDO.setCreateTime(LocalDateTime.now());
        gameDO.setCreateBy(StpKit.ADMIN.getLoginIdAsLong());
        gameDO.setUpdateBy(StpKit.ADMIN.getLoginIdAsLong());
        // 保存到数据库
        int insert = this.getMapper().insert(gameDO);
        return SaResult.data(insert);
    }

    @Override
    @Transient
    public SaResult deleteGame(Long id) {
        //删除赛事
        int delete = this.getMapper().deleteById(id);
        if (delete > 0) {
            //删除赛事下的所有报价记录
            gameRegistrationMapper.deleteByQuery(new QueryWrapper().eq(GameRegistrationDO::getGameId, id));
            // 删除 队伍记录
            gameTeamMapper.deleteByQuery(new QueryWrapper().eq(GameTeamDO::getGameId, id));
            //删除 PK 记录
            gamePKMapper.deleteByQuery(new QueryWrapper().eq(GamePKDO::getGameId, id));
        }
        return SaResult.data(delete > 0);
    }

    @Override
    public SaResult editGame(GameEditDTO dto) {
        //赛事编辑
        GameDO gameDO = GameConverter.INSTANCE.doToDO(dto);
        gameDO.setId(dto.getGameId());
        int insert = this.getMapper().update(gameDO);
        return SaResult.data(insert > 0);
    }


    @Override
    public SaResult getGameDetail(Long id) {
        //获取赛事详情
        GameDO gameDO = this.getMapper().selectOneById(id);
        if (gameDO == null) {
            return SaResult.error("数据不存在");
        }
        GameVO vo = GameConverter.INSTANCE.doToVO(gameDO);
        return SaResult.data(vo);
    }


    @Override
    public SaResult getTodayGame() {
        //当天赛事
        //构建查询条件 当天00:00:00 - 当天23:59:59
        LocalDateTime startTime = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endTime = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(GameDO::getStatus, 1);
        queryWrapper.between(GameDO::getStartTime, startTime, endTime);
        GameDO gameDO = this.getMapper().selectOneByQuery(queryWrapper);
        if (gameDO != null) {
            return SaResult.data(GameConverter.INSTANCE.doToVO(gameDO));
        }
        return SaResult.error("数据不存在");
    }


    @Override
    public SaResult changeState(GameStateDO data) {
        //赛事状态修改
        GameDO gameDO = this.getMapper().selectOneById(data.getGameId());
        if (gameDO == null) {
            return SaResult.error("数据不存在");
        }
        gameDO.setStatus(data.getStatus());
        int update = this.getMapper().update(gameDO);
        if (update > 0) {
            if (data.getStatus() == 2) {

            } else if (data.getStatus() == 3) {



            }
            return SaResult.error("数据不存在");
        }
        return SaResult.data(update > 0);
    }


}